import Toast from '@vant/weapp/toast/toast';
import request from './request.js';
// App()由小程序原生提供，它是一个函数，表示新建一个小程序实例
// 它的参数是一个配置对象，用于设置小程序实例的行为属性。
App({
  //全局变量
  globalData: {
    // server: "http://192.168.0.9:8090/",
    server: new request()._baseUrl,
    myUserInfo: {},
    weChatUserInfo: {}
  },
  data: {

  },
  myRequest() {
    return new request();
  },
  wxLogin() {
    return new Promise(function (reslove, reject) {
      wx.login({
        success(res) {
          reslove(res.code);
        }
      })
    })
  },
  registOrLoginPromise:async function () {
    let self = this;
    var code = await self.wxLogin();
    //var info = await this.login(code);
    let promise = new request().postRequest('wchat/login', {
      "code": code
    })
    return promise;
  },
  registOrLogin: function (successBack) {
    let self = this;
    const promise = new Promise(function (resolve, reject) {
      wx.login({
        success(res) {
          if (res.code) {
            wx.request({
              url: self.globalData.server + "wchat/login",
              method: "post",
              data: {
                "code": res.code
              },
              dataType: "json",
              responseType: "	text",
              success(result) {
                self.globalData.myUserInfo = result.data.data;
                console.log("调用登录接口成功!");
                if (!!successBack) {
                  successBack();
                }
              },
              fail(result) {
                console.log("调用登录接口失败!");
                Toast('调用登录接口失败,请稍后重试!')
                console.log(result);
              },
              complete() {
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    })
    return promise;
  },
})
